package com.mdb.open.trade.facade.trade;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.autoconfigure.database.MdbDataSource;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.enums.PlatformRetCodeEnum;
import com.mdb.open.common.enums.DataChannelEnum;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.common.enums.TradeWebErrorCodeEnum;
import com.mdb.open.common.enums.trade.status.*;
import com.mdb.open.common.enums.trade.type.AmlApiTypeEnum;
import com.mdb.open.common.enums.trade.type.ProcessLogTypeEnum;
import com.mdb.open.trade.config.AccountingDateUtil;
import com.mdb.open.trade.domain.bo.trade.*;
import com.mdb.open.trade.domain.po.trade.*;
import com.mdb.open.trade.domain.vo.trade.SubmitApplyResultVo;
import com.mdb.open.trade.feign.OutreachClient;
import com.mdb.open.trade.feign.dto.CargoScanDto;
import com.mdb.open.trade.feign.dto.CargoScanListDto;
import com.mdb.open.trade.feign.dto.TradeFinanceBlacklistScanDto;
import com.mdb.open.trade.feign.dto.TradeFinanceBlacklistScanListDto;
import com.mdb.open.trade.feign.vo.CargoScanVo;
import com.mdb.open.trade.feign.vo.TradeFinanceBlacklistScanVo;
import com.mdb.open.trade.service.trade.*;
import com.mdb.open.trade.utils.AmountCalcUtils;
import com.mdb.open.trade.utils.ConvertFileVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @author: you.yichao
 * @create: 2019-07-08 10:35
 **/
@Service
public class LoanApplyFlowFacade {

    @Autowired
    TfLoanProcessLogService tfLoanProcessLogService;

    @Autowired
    TfLoanApplyBaseInfoService tfLoanApplyBaseInfoService;

    @Autowired
    TfLoanContractService tfLoanContractService;

    @Autowired
    TfMerchantBankAccountService tfMerchantBankAccountService;

    @Autowired
    SequenceGenerator sequenceGenerator;

    @Autowired
    TfMerAccService tfMerAccService;

    @Autowired
    TfLoanApplyBaseInfoService loanApplyBaseInfoService;

    @Autowired
    TfTradeFileBasicInfoService tfTradeFileBasicInfoService;

    @Autowired
    TfTradeFileCustomsInfoService tradeFileCustomsInfoService;

    @Autowired
    TfTradeFileExtendInfoService tradeFileExtendInfoService;

    @Autowired
    TfTradeFileItemInfoService tradeFileItemInfoService;

    @Autowired
    TfTradeFileTransportInfoService tradeFileTransportInfoService;

    @Autowired
    TfAttachFileService attachFileService;

    @Autowired
    TfLoanApplyBasicInfoService loanApplyBasicInfoService;

    @Autowired
    TfLoanApplyCustomsInfoService loanApplyCustomsInfoService;

    @Autowired
    TfLoanApplyExtendInfoService loanApplyExtendInfoService;

    @Autowired
    TfLoanApplyTransportInfoService loanApplyTransportInfoService;

    @Autowired
    TfLoanApplyItemInfoService loanApplyItemInfoService;

    @Autowired
    ConvertFileVoUtil convertFileVoUtil;

    @Autowired
    AccountingDateUtil accountingDateUtil;

    @Autowired
    OutreachClient outreachClient;

    @Autowired
    TfAmlLogService tfAmlLogService;

    /**
     * 节点：创建融资申请单,保存节点时序信息
     */
    @DS(MdbDataSource.MASTER)
    @Transactional
    @LogAnnotation
    public ResultEx<SubmitApplyResultVo> createApplyOrder(LoanApplyBo createBo) {
        SubmitApplyResultVo vo = new SubmitApplyResultVo();

        LoanApplyBaseInfo loanApplyBaseInfo = new LoanApplyBaseInfo();
        LocalDateTime now = accountingDateUtil.getAccountingDateTime();

        //获取额外信息
        TradeFileExtendInfo extendInfo = tradeFileExtendInfoService.getInfoByNo(createBo.getTradeFileNo()).getData();
        //收款账号信息
        MerchantBankAccount account = tfMerchantBankAccountService.getAccountInfoByAccUniNo(createBo.getAccUniqueNo(),
                createBo.getCustomerCode(), createBo.getMerchantNo()).getData();
        if(ObjectUtil.isEmpty(account)){
            return ResultEx.makeResult(PlatformRetCodeEnum.ACCOUNT_QUERY_EMPTY);
        }
        //获取平台子账号
        GetMerAccBo sccBo = new GetMerAccBo();
        sccBo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        sccBo.setMerchantNo(createBo.getMerchantNo());
        MerAcc acc = tfMerAccService.getByMerchantNo(sccBo).getData();
        if(ObjectUtil.isEmpty(acc)){
            return ResultEx.makeResult(PlatformRetCodeEnum.PLATFORMACC_QUERY_EMPTY);
        }

        //保存融资单状态
        loanApplyBaseInfo.setMerchantNo(createBo.getMerchantNo());
        loanApplyBaseInfo.setMerchantName(createBo.getMerchantName());
        loanApplyBaseInfo.setBusinessNo(createBo.getBusinessNo());
        loanApplyBaseInfo.setFinanceAmount(createBo.getCustomsTotalAmount());
        loanApplyBaseInfo.setExpectedAmount(AmountCalcUtils.calcExpectedApplyAmount(createBo.getBaseAmount(), ObjectUtil.isEmpty(extendInfo) ? null : extendInfo.getDepositAmount()));
        loanApplyBaseInfo.setCcy(createBo.getCcy());
        loanApplyBaseInfo.setReceiveAccCnName(account.getAccCnName());
        loanApplyBaseInfo.setReceiveAccEnName(account.getAccEnName());
        loanApplyBaseInfo.setReceiveAddress(account.getReceiveAddressEn());
        loanApplyBaseInfo.setReceiveAccNo(account.getAccNo());
        loanApplyBaseInfo.setReceiveAccBranch(account.getAccBranch());
        loanApplyBaseInfo.setSwiftCode(account.getRecevSwiftCode());
        loanApplyBaseInfo.setCustomerName(createBo.getBasicInfo().getCustomerName());
        loanApplyBaseInfo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        loanApplyBaseInfo.setMcustomerCode(createBo.getBasicInfo().getMcustomerCode());
        loanApplyBaseInfo.setPlatformAcc(acc.getPlatformAcc());
        loanApplyBaseInfo.setTradeFileNo(createBo.getTradeFileNo());
        loanApplyBaseInfo.setApplyOrderNo(sequenceGenerator.generator(TfSequenceEnum.APPLY_NO.setDataChannelEnum(DataChannelEnum.API)));
        loanApplyBaseInfo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        loanApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.NOT_INITIATED);
        loanApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.UN_REVIEW);
        loanApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.UN_REVIEW);
        loanApplyBaseInfo.setAmlStatus(LoanApplyAmlStatusEnum.UN_REVIEW);
        loanApplyBaseInfo.setApplyTime(now);
        loanApplyBaseInfo.setBusinessType(createBo.getBusinessType());

        //保存业务基本信息
        boolean result = loanApplyBaseInfoService.save(loanApplyBaseInfo);
        if(Boolean.FALSE.equals(result)){
            return ResultEx.fail();
        }

        vo.setExpectedAmount(loanApplyBaseInfo.getExpectedAmount());
        vo.setFinanceAmount(loanApplyBaseInfo.getFinanceAmount());
        vo.setCcy(loanApplyBaseInfo.getCcy());

        //保存业务基本资料
        LoanApplyBasicInfoBo loanApplyBasicInfoBo = new LoanApplyBasicInfoBo();
        loanApplyBasicInfoBo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
        loanApplyBasicInfoBo.setMcustomerCode(createBo.getBasicInfo().getMcustomerCode());
        loanApplyBasicInfoBo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        loanApplyBasicInfoBo.setCustomerName(createBo.getBasicInfo().getCustomerName());
        loanApplyBasicInfoBo.setMerchantNo(createBo.getBasicInfo().getMerchantNo());
        loanApplyBasicInfoBo.setMerchantName(createBo.getBasicInfo().getMerchantName());
        loanApplyBasicInfoBo.setBusinessNo(createBo.getBasicInfo().getBusinessNo());
        loanApplyBasicInfoBo.setBusinessType(createBo.getBasicInfo().getBusinessType());
        loanApplyBasicInfoBo.setImporterCnName(createBo.getBasicInfo().getImporterCnName());
        loanApplyBasicInfoBo.setImporterEnName(createBo.getBasicInfo().getImporterEnName());
        loanApplyBasicInfoBo.setImporterAddress(createBo.getBasicInfo().getImporterAddress());
        loanApplyBasicInfoBo.setCustomsCnt(createBo.getBasicInfo().getCustomsCnt());
        loanApplyBasicInfoBo.setCustomsAmount(createBo.getBasicInfo().getCustomsAmount());
        loanApplyBasicInfoBo.setContractAmount(createBo.getBasicInfo().getContractAmount());
        loanApplyBasicInfoBo.setContractCcy(createBo.getBasicInfo().getContractCcy());
        loanApplyBasicInfoBo.setItemsAmount(createBo.getBasicInfo().getItemsAmount());
        loanApplyBasicInfoBo.setCustomsTime(createBo.getBasicInfo().getCustomsTime());
        loanApplyBasicInfoBo.setStatus(createBo.getBasicInfo().getStatus());
        loanApplyBasicInfoBo.setSourceType(createBo.getBasicInfo().getSourceType());
        loanApplyBasicInfoBo.setApplyTime(now);
        ResultEx<LoanApplyBasicInfo> loanApplyBasicInfoResultEx = loanApplyBasicInfoService.saveBasicInfo(loanApplyBasicInfoBo);
        if(loanApplyBasicInfoResultEx.isFailed()){
            return ResultEx.makeResult(PlatformRetCodeEnum.MODULE_SAVE_FAIL);
        }

        //保存运输信息
        ListResultEx<TradeFileTransportInfo> transportInfoEx = tradeFileTransportInfoService.queryInfoByNo(createBo.getTradeFileNo());
        if (transportInfoEx.isFailed() || ObjectUtil.isEmpty(transportInfoEx.getData())) {
            return ResultEx.fail(TradeWebErrorCodeEnum.MODULE_SAVE_FAIL.getText());
        }
        for (TradeFileTransportInfo transportInfo : transportInfoEx.getData()) {
            LoanApplyTransportInfoBo applyTransportInfo = new LoanApplyTransportInfoBo();
            applyTransportInfo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
            applyTransportInfo.setApplyOrderCustomsNo(transportInfo.getTradeFileCustomsNo());
            applyTransportInfo.setCustomsNo(transportInfo.getCustomsNo());
            applyTransportInfo.setBusinessNo(transportInfo.getBusinessNo());
            applyTransportInfo.setMcustomerCode(transportInfo.getMcustomerCode());
            applyTransportInfo.setCustomerCode(transportInfo.getCustomerCode());
            applyTransportInfo.setCustomerName(transportInfo.getCustomerName());
            applyTransportInfo.setMerchantNo(transportInfo.getMerchantNo());
            applyTransportInfo.setMerchantName(transportInfo.getMerchantName());
            applyTransportInfo.setLadingBillNo(transportInfo.getLadingBillNo());
            applyTransportInfo.setContainerNo(transportInfo.getContainerNo());
            applyTransportInfo.setShipper(transportInfo.getShipper());
            applyTransportInfo.setReceiverCnName(transportInfo.getReceiverCnName());
            applyTransportInfo.setReceiverEnName(transportInfo.getReceiverEnName());
            applyTransportInfo.setShipLoadTime(transportInfo.getShipLoadTime());
            applyTransportInfo.setReceiverCountry(transportInfo.getReceiverCountry());
            applyTransportInfo.setTransportName(transportInfo.getTransportName());
            applyTransportInfo.setTransportType(transportInfo.getTransportType());
            applyTransportInfo.setStatus(transportInfo.getStatus());
            applyTransportInfo.setSourceType(transportInfo.getSourceType());
            ResultEx transportInfoResult = loanApplyTransportInfoService.saveTransportInfo(applyTransportInfo);
            if(transportInfoResult.isFailed()){
                return ResultEx.fail(TradeWebErrorCodeEnum.MODULE_SAVE_FAIL.getText());
            }
        }

        //保存报关信息
        List<LoanApplyCustomsInfo> batchCustomsInfoList = Lists.newArrayList();
        for (TradeFileCustomsInfo customsInfo : createBo.getCustomsInfoList()) {

            LoanApplyCustomsInfo applyCustomsInfo = new LoanApplyCustomsInfo();
            applyCustomsInfo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
            applyCustomsInfo.setApplyOrderCustomsNo(customsInfo.getTradeFileCustomsNo());
            applyCustomsInfo.setBusinessNo(customsInfo.getBusinessNo());
            applyCustomsInfo.setCustomerCode(customsInfo.getCustomerCode());
            applyCustomsInfo.setMcustomerCode(customsInfo.getMcustomerCode());
            applyCustomsInfo.setCustomerName(customsInfo.getCustomerName());
            applyCustomsInfo.setMerchantNo(customsInfo.getMerchantNo());
            applyCustomsInfo.setCustomsNo(customsInfo.getCustomsNo());
            applyCustomsInfo.setMerchantName(customsInfo.getMerchantName());
            applyCustomsInfo.setCustomsLocation(customsInfo.getCustomsLocation());
            applyCustomsInfo.setCustomsTitle(customsInfo.getCustomsTitle());
            applyCustomsInfo.setCustomsAmount(customsInfo.getCustomsAmount());
            applyCustomsInfo.setCcy(customsInfo.getCcy());
            applyCustomsInfo.setCommitTime(customsInfo.getCommitTime());
            applyCustomsInfo.setLoadingPort(customsInfo.getLoadingPort());
            applyCustomsInfo.setLoadingPortCode(customsInfo.getLoadingPortCode());
            applyCustomsInfo.setDestinPort(customsInfo.getDestinPort());
            applyCustomsInfo.setDestinPortCode(customsInfo.getDestinPortCode());
            applyCustomsInfo.setDestinCountry(customsInfo.getDestinCountry());
            applyCustomsInfo.setStatus(customsInfo.getStatus());
            applyCustomsInfo.setSourceType(customsInfo.getSourceType());
            batchCustomsInfoList.add(applyCustomsInfo);

        }
        boolean saveFlag = loanApplyCustomsInfoService.saveBatch(batchCustomsInfoList);
        if(!saveFlag){
            return ResultEx.makeResult(PlatformRetCodeEnum.MODULE_SAVE_FAIL);
        }

        //保存额外信息
        if (ObjectUtil.isNotEmpty(extendInfo)) {
            LoanApplyExtendInfoBo applyExtendInfo = new LoanApplyExtendInfoBo();
            applyExtendInfo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
            applyExtendInfo.setBusinessNo(extendInfo.getBusinessNo());
            applyExtendInfo.setCustomerCode(extendInfo.getCustomerCode());
            applyExtendInfo.setMcustomerCode(extendInfo.getMcustomerCode());
            applyExtendInfo.setCustomerName(extendInfo.getCustomerName());
            applyExtendInfo.setMerchantNo(extendInfo.getMerchantNo());
            applyExtendInfo.setMerchantName(extendInfo.getMerchantName());
            applyExtendInfo.setDepositAmount(extendInfo.getDepositAmount());
            applyExtendInfo.setDepositCcy(extendInfo.getDepositCcy());
            applyExtendInfo.setDepositPercent(extendInfo.getDepositPercent());
            applyExtendInfo.setInsuranceAmount(extendInfo.getInsuranceAmount());
            applyExtendInfo.setInsuranceCcy(extendInfo.getInsuranceCcy());
            applyExtendInfo.setInsurancePercent(extendInfo.getInsurancePercent());
            applyExtendInfo.setStatus(extendInfo.getStatus());
            applyExtendInfo.setSourceType(extendInfo.getSourceType());
            ResultEx extendInfoResult = loanApplyExtendInfoService.saveExtendInfo(applyExtendInfo);
            if(extendInfoResult.isFailed()){
                return ResultEx.makeResult(PlatformRetCodeEnum.MODULE_SAVE_FAIL);
            }
        }

        //保存商品信息
        List<TradeFileItemInfo> itemList = tradeFileItemInfoService.queryItemListByNo(createBo.getTradeFileNo()).getData();
        List<LoanApplyItemInfoBo> boList = Lists.newArrayList();
        if(ObjectUtil.isNotEmpty(itemList) && itemList.size() > 0){
            itemList.forEach(i -> {
                LoanApplyItemInfoBo bo = new LoanApplyItemInfoBo();
                bo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
                bo.setApplyOrderCustomsNo(i.getTradeFileCustomsNo());
                bo.setCustomsNo(i.getCustomsNo());
                bo.setItemNo(i.getTradeFileItemNo());
                bo.setCustomerCode(i.getCustomerCode());
                bo.setMcustomerCode(i.getMcustomerCode());
                bo.setCustomerName(i.getCustomerName());
                bo.setMerchantNo(i.getMerchantNo());
                bo.setMerchantName(i.getMerchantName());
                bo.setBusinessNo(i.getBusinessNo());
                bo.setItemCnName(i.getItemCnName());
                bo.setItemEnName(i.getItemEnName());
                bo.setHsCode(i.getHsCode());
                bo.setBrand(i.getBrand());
                bo.setUnit(i.getUnit());
                bo.setQuantity(i.getQuantity());
                bo.setContractAmount(i.getContractAmount());
                bo.setContractCcy(i.getContractCcy());
                bo.setCustomUnitAmount(i.getCustomUnitAmount());
                bo.setCustomUnitCcy(i.getCustomUnitCcy());
                bo.setCustomTotalAmount(i.getCustomTotalAmount());
                bo.setCustomTotalCcy(i.getCustomTotalCcy());
                bo.setStatus(i.getStatus());
                bo.setSourceType(i.getSourceType());

                boList.add(bo);
            });
        }

        ResultEx itemResult = loanApplyItemInfoService.saveItemInfo(boList);
        if(itemResult.isFailed()){
            return ResultEx.makeResult(PlatformRetCodeEnum.MODULE_SAVE_FAIL);
        }

        //添加时序节点: 提交申请
        SaveLoanProcessLogBo submitLogBo = new SaveLoanProcessLogBo();
        submitLogBo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
        submitLogBo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        submitLogBo.setMerchantNo(createBo.getMerchantNo());
        submitLogBo.setType(ProcessLogTypeEnum.APPLY_COMMIT);
        submitLogBo.setFinishDate(now);

        UpdateLoanApplyBo submitApplyBo = new UpdateLoanApplyBo();
        submitApplyBo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
        submitApplyBo.setStatus(ProcessLogTypeEnum.APPLY_COMMIT);

        ProcessLoanApplyFlowBo submitFlowBo = new ProcessLoanApplyFlowBo();
        submitFlowBo.setSaveLoanProcessLogBo(submitLogBo);
        submitFlowBo.setUpdateLoanApplyBo(submitApplyBo);

        TradeFileTempBasicInfoBo basicInfoBo = new TradeFileTempBasicInfoBo();
        basicInfoBo.setTradeFileNo(createBo.getTradeFileNo());
        basicInfoBo.setCustomerCode(createBo.getBasicInfo().getCustomerCode());
        basicInfoBo.setMerchantNo(createBo.getMerchantNo());

        ResultEx processResult = this.processLoanApplyFlow(submitFlowBo, basicInfoBo);
        if(processResult.isFailed()){
            return processResult;
        }

        vo.setApplyOrderNo(loanApplyBaseInfo.getApplyOrderNo());
        return ResultEx.ok(vo);
    }

    /**
     * 更新贸易融资申请单状态，新增对应的时序记录
     *
     * @param bo
     * @return
     */
    @DS(MdbDataSource.MASTER)
    @Transactional
    @LogAnnotation
    public ResultEx processLoanApplyFlow(ProcessLoanApplyFlowBo bo, TradeFileTempBasicInfoBo basicInfoBo) {

        //更新融资申请单状态
        ResultEx updateResult = tfLoanApplyBaseInfoService.updateLoanApplyStatus(bo.getUpdateLoanApplyBo());
        if(updateResult.isFailed()){
            return ResultEx.makeResult(PlatformRetCodeEnum.APPLY_STATUS_UPDATE_FAIL);
        }

        //新增对应的时序记录
        ResultEx saveResult = tfLoanProcessLogService.saveLoanProcessLog(bo.getSaveLoanProcessLogBo());
        if(saveResult.isFailed()){
            return ResultEx.makeResult(PlatformRetCodeEnum.PROCESS_LOG_ADD_FAIL);
        }

        //更新贸易档案记录
        ResultEx updateTradeFileEx = tfTradeFileBasicInfoService.updateBasicInfoWhenCommitApply(basicInfoBo, bo.getUpdateLoanApplyBo().getApplyOrderNo());
        if(updateTradeFileEx.isFailed()){
            return ResultEx.makeResult(PlatformRetCodeEnum.APPLY_STATUS_UPDATE_FAIL);
        }


        return ResultEx.ok();
    }


    @Transactional
    public ResultEx tradeCallAml(TradeCallAmlBo bo) {

        CargoScanDto cargoScanDto = new CargoScanDto();
        CargoScanListDto cargoScanListDto = new CargoScanListDto();
        cargoScanListDto.setCargoInfo(bo.getCargoDtoList());
        cargoScanDto.setBody(cargoScanListDto);

        ResultEx<CargoScanVo> cargoEx = outreachClient.cargoScan(cargoScanDto);
        if (cargoEx.isFailed()) {
            return cargoEx;
        }

        boolean hitFlag = false;

        AmlLog cargoAml = new AmlLog();
        cargoAml.setApplyOrderNo(bo.getApplyOrderNo());
        cargoAml.setAmlSeqNo(cargoEx.getData().getCode());
        cargoAml.setApiType(AmlApiTypeEnum.CARGO);
        cargoAml.setStatus(AmlResultStatusEnum.PASS);
        cargoAml.setAlertNo(cargoEx.getData().getAlertNo());
        cargoAml.setSimilarity(cargoEx.getData().getSimilarity());
        cargoAml.setViolations(JSON.toJSONString(cargoEx.getData().getViolations()));
        if (AmlHitStatusEnum.SUSPECT.getValue().equals(cargoEx.getData().getAlertNo())) {
            hitFlag = true;
            cargoAml.setStatus(AmlResultStatusEnum.UN_BACK);
        }
        ResultEx saveEx = tfAmlLogService.saveAmlLog(cargoAml);
        if (saveEx.isFailed()) {
            return saveEx;
        }


        for (TradeFinanceBlacklistScanListDto scanListDto : bo.getBlacklistScanListDtos()) {
            TradeFinanceBlacklistScanDto blacklistScanDto = new TradeFinanceBlacklistScanDto();
            blacklistScanDto.setBody(scanListDto);

            ResultEx<TradeFinanceBlacklistScanVo> scanVoResultEx = outreachClient.tradeFinanceBlacklistScan(blacklistScanDto);
            if (scanVoResultEx.isFailed()) {
                return scanVoResultEx;
            }

            cargoAml = new AmlLog();
            cargoAml.setApplyOrderNo(bo.getApplyOrderNo());
            cargoAml.setAmlSeqNo(scanVoResultEx.getData().getCode());
            cargoAml.setStatus(AmlResultStatusEnum.PASS);
            cargoAml.setApiType(AmlApiTypeEnum.TRADE);
            cargoAml.setAlertNo(scanVoResultEx.getData().getAlertNo());
            cargoAml.setSimilarity(scanVoResultEx.getData().getSimilarity());
            cargoAml.setViolations(JSON.toJSONString(scanVoResultEx.getData().getViolations()));
            if (AmlHitStatusEnum.SUSPECT.getValue().equals(scanVoResultEx.getData().getAlertNo())) {
                cargoAml.setStatus(AmlResultStatusEnum.UN_BACK);
                hitFlag = true;
            }
            saveEx = tfAmlLogService.saveAmlLog(cargoAml);
            if (saveEx.isFailed()) {
                return saveEx;
            }


        }

        UpdateLoanApplyBo applyBo = new UpdateLoanApplyBo();
        applyBo.setApplyOrderNo(bo.getApplyOrderNo());
        if (hitFlag) {
            applyBo.setStatus(ProcessLogTypeEnum.AML_PROCESSING);
        } else {
            applyBo.setStatus(ProcessLogTypeEnum.AML_APPROVE_SUCCESS);
        }

        ResultEx applyEx = tfLoanApplyBaseInfoService.updateLoanApplyStatus(applyBo);
        if (applyEx.isFailed()) {
            return applyEx;
        }

        return ResultEx.ok();
    }


}
